//
//  TimelineViewController.m
//  aMoment
//
//  Created by dllo on 15/10/4.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "TimelineViewController.h"
#import "AllHeaderFiles.h"

@interface TimelineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, assign)CGFloat imgHeight;
@property(nonatomic, assign)NSInteger start;
@property(nonatomic, retain)MBProgressHUD *progressView;
@end

@implementation TimelineViewController

- (void)dealloc
{
    [_tableView release];
    [_progressView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"碎片";
    self.dataSourceArray = [NSMutableArray array];
    [self getData];
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _start = 0;
        [self getData];
    }];
//    [_tableView.header beginRefreshing];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _start += 10;
        [self getData];
    }];
}

#pragma mark 获取数据
- (void)getData
{
    NSString *bodyString = [NSString stringWithFormat:@"start=%ld&limit=10&client=2", _start];
    [NetworkRequest getDataWithURLString:URLSTRING_TIMELINE BodyString:bodyString DataBlock:^(id data) {
        //移除加载中提示视图
        [_progressView removeFromSuperview];
        
        if (!_start) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *listArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            Timeline *timeline = [[Timeline alloc] init];
            [timeline setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:timeline];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

- (void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.Width, self.view.Height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    //创建加载中提示视图
    self.progressView = [LoadingProgress createViewWithSuperviewFrame:self.view.frame];
    [self.view addSubview:_progressView];
    [_progressView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    TimelineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TimelineTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSNumber *contentHeight = [NSNumber numberWithDouble:[self getStringHeightWithDataString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - (40 + 32 + 20) * FIT_WIDTH)]];
    NSMutableDictionary *timelineDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.dataSourceArray[indexPath.row], @"timeline", contentHeight, @"contentHeight", nil];
    if (![[self.dataSourceArray[indexPath.row] coverimg] isEqualToString:@""]) {
        NSNumber *imgHeight = [NSNumber numberWithDouble:[self getImgHeightWithImgSizeString:[self.dataSourceArray[indexPath.row] coverimg_wh]]];
        [timelineDict setObject:imgHeight forKey:@"imgHeight"];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.timelineDict = timelineDict;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![[self.dataSourceArray[indexPath.row] coverimg] isEqualToString:@""]) {
        return [self getStringHeightWithDataString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - (40 + 32 + 10) * FIT_WIDTH)] + [self getImgHeightWithImgSizeString:[self.dataSourceArray[indexPath.row] coverimg_wh]] + 101 * FIT_HEIGHT + 32 * FIT_WIDTH;
    }
    return [self getStringHeightWithDataString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - (40 + 32 + 10) * FIT_WIDTH)] + 101 * FIT_HEIGHT + 32 * FIT_WIDTH - 15 * FIT_HEIGHT;
}

#pragma mark 获取字符串高度
- (CGFloat)getStringHeightWithDataString:(NSString *)dataString
                          LabelWidth:(CGFloat)labelWidth
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONTSIZE_MIDDLE], NSFontAttributeName, nil];
    CGRect rect = [dataString boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.height + 1;
}

#pragma mark 获取图片高度
- (CGFloat)getImgHeightWithImgSizeString:(NSString *)imgSizeString
{
    NSArray *array = [imgSizeString componentsSeparatedByString:@"*"];
    return (SCREEN_WIDTH - 40 * FIT_WIDTH) * [array[1] doubleValue] / [array[0] doubleValue];
}

#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TimelineDetailsViewController *detailsVC = [[TimelineDetailsViewController alloc] init];
    [self.navigationController pushViewController:detailsVC animated:YES];
    detailsVC.contentId = [self.dataSourceArray[indexPath.row] contentid];
    [detailsVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
